<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
use think\exception\DbException;
use think\response\Json;
use \app\admin\model\Pinxi;

/**
 * 厂区生物库明细
 *
 * @icon fa fa-circle-o
 */
class Storepro extends Backend
{

    /**
     * Storepro模型对象
     * @var \app\admin\model\Storepro
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Storepro;
        $this->view->assign("statusList", $this->model->getStatusList());

        //  采购入库 调拨入库 生产入库 调拨出库 死亡出库 淘汰出库 销售出库



    }


    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        $list = $this->getXXX($list);

        $pinxiList = Pinxi::getList();

        foreach($list->items() as &$val)
        {
            $val['storea_id'] = Db::name("storea")->where("id", $val['storea_id'])->find()['name'];
            $val['field2'] = isset($pinxiList[$val['field2']]) ? $pinxiList[$val['field2']] : "";

            //$val['storesub_id'] = Db::name("storesub")->where("id", $val['storesub_id'])->find()['name'];
            //$val['store1_id'] = Db::name("store1")->where("id", $val['store1_id'])->find()['name'];
            //$val['storem1_id'] = Db::name("storem1")->where("id", $val['storem1_id'])->find()['name'];
        }

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }



    /**
     * 查看库位对应的批次及数量
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function batchlist()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //[$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $factory = $this->request->get('factory_id');
        $storea = $this->request->get('storea_id');
        $list = $this->model->getStock($storea, $factory);

        $list = $this->getXXX($list);

        $pinxiList = Pinxi::getList();
        foreach($list as $k=> &$val){
            $val['field2'] = isset($pinxiList[$val['field2']]) ? $pinxiList[$val['field2']] : "";
        }

        $result = ['total' => "", 'rows' => $list ];
        return json($result);
    }

    /**
     * 查看库位对应的批次及数量--- 按品系
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function pinxilist()
    {
        // 设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        // [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $factory = $this->request->get('factory_id');
        $storea = $this->request->get('storea_id');
        $list = $this->model->getStockByPinxi($storea, $factory);

        $list = $this->getXXX($list);

        $pinxiList = Pinxi::getList();
        foreach($list as $k=> &$val){
            $val['field2'] = isset($pinxiList[$val['field2']]) ? $pinxiList[$val['field2']] : "";

            $storea = isset($val['storea_id']) ? Db::name("storea")->where('id', $val['storea_id'])->find() : [];

            $val['storea_id'] = $storea ? $storea['name'] : "";
        }

        $result = ['total' => "", 'rows' => $list ];
        return json($result);
    }


}
